//
//  SHHGetUser.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/5.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class SHHGetUser: NSObject {
    
    var name:String?  //当前用户名
    var user_id:Int? //用户ID
    var photo_url:String? //头像地址
    var gender:String? //性别
    
    
    private override init() {}
    static var sharedGetUser:SHHGetUser? = SHHGetUser()
    
    //返回一个当前用户
    func getUserInfo()-> SHHGetUser {
        let user = AVUser.currentUser()
        
        let query = AVQuery(className: "UserInfo")
        query.whereKey("user_id", equalTo: user["user_id"])
        let userArray = query.findObjects()
        if userArray.isEmpty || userArray == nil {
            print("数组没有纸")
            SHHGetUser.sharedGetUser?.name = user.username
        }else{
            let resultUser = userArray.first
            SHHGetUser.sharedGetUser!.name = resultUser!["name"] as? String
            SHHGetUser.sharedGetUser!.photo_url = resultUser!["photo_url"] as? String
            SHHGetUser.sharedGetUser!.gender = resultUser!["gender"] as? String
            SHHGetUser.sharedGetUser!.user_id = resultUser!["user_id"] as? Int
        }
        
       
        
        return SHHGetUser.sharedGetUser!
    }
    
    func updateUserInfo() {
        let user = AVUser.currentUser()
        
        if user == nil {
            return
        }else{
            let query = AVQuery(className: "UserInfo")
            query.whereKey("user_id", equalTo: user["user_id"])
            let userArray = query.findObjects()
            let resultUser = userArray.first
            self.name = resultUser!["name"] as? String
            self.photo_url = resultUser!["photo_url"] as? String
            self.gender = resultUser!["gender"] as? String
            self.user_id = resultUser!["user_id"] as? Int
        }
    }
    
    
}
